package org.dk.poitalk.model;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

import org.springframework.stereotype.Repository;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.jappstart.model.auth.UserAccount;

@SuppressWarnings("serial")
@Repository
@Entity
public class EntityClient implements Serializable, DomainUserEntity {

    @Id
    private Key key;
	private String idPlace;
	@Enumerated(EnumType.STRING)
	private TipoEntityClient tipoEntityClient;

    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private UserAccount userAccount;
    
	public EntityClient(String idPlace, TipoEntityClient tipoEntityClient, UserAccount userAccount) {
		super();
		this.idPlace = idPlace;
		this.tipoEntityClient = tipoEntityClient;
		this.userAccount = userAccount;
	}
	
	public EntityClient(Key key, String idPlace,
			TipoEntityClient tipoEntityClient, UserAccount userAccount) {
		super();
		this.key = key;
		this.idPlace = idPlace;
		this.tipoEntityClient = tipoEntityClient;
		this.userAccount = userAccount;
	}	
	
	public static Key getNewKeyExternal(Key keyExternal, String idPlace) {
		return KeyFactory.createKey(keyExternal,EntityClient.class.getSimpleName(), idPlace);
	}	
	
	public String getIdPlace() {
		return idPlace;
	}
	public void setIdPlace(String idPlace) {
		this.idPlace = idPlace;
	}

	public Key getKey() {
		return key;
	}
	public void setKey(Key key) {
		this.key = key;
	}
	public TipoEntityClient getTipoEntityClient() {
		return tipoEntityClient;
	}
	public void setTipoEntityClient(TipoEntityClient tipoEntityClient) {
		this.tipoEntityClient = tipoEntityClient;
	}

	@Override
	public String toString() {
		return "EntityClient [key=" + key + ", idPlace=" + idPlace
				+ ", tipoEntityClient=" + tipoEntityClient.toString();
				//+ ", userAcccount= " + ((userAccount != null)?userAccount.toString():"null") + "]";
	}

	public UserAccount getUserAccount() {
		return userAccount;
	}

	public void setUserAccount(UserAccount userAccount) {
		this.userAccount = userAccount;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((idPlace == null) ? 0 : idPlace.hashCode());
		result = prime * result + ((key == null) ? 0 : key.hashCode());
		result = prime
				* result
				+ ((tipoEntityClient == null) ? 0 : tipoEntityClient.hashCode());
		result = prime * result
				+ ((userAccount == null) ? 0 : userAccount.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		EntityClient other = (EntityClient) obj;
		if (idPlace == null) {
			if (other.idPlace != null)
				return false;
		} 
		return true;
	}
	

}
